package com.yunsom.testdemo.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PageUtil
 * @Description
 * @Author cxb
 * @Date 2023-4-18 10:57
 * @Version 1.0.0
 */
public class PageUtil {

    /**
     * 获取分页结果
     * @param page
     * @param perPage
     * @param list
     * @return
     */
    public static List<?> getPagedList(int page, int perPage, List<?> list){

        if (page < 0 || (page-1) * perPage >= list.size() || perPage <= 0) {
            return new ArrayList<>();
        }
        int lastIndex = (page-1) * perPage + perPage;
        if (lastIndex > list.size()) {
            lastIndex = list.size();
        }

        //获得分页后的list
        list = list.subList((page-1) * perPage, lastIndex);

        return list;
    }

}
